package odgs.transaction;

import java.util.ArrayList;
import java.util.List;

import odgs.Contents;
import odgs.ExplorerNode;
import odgs.Transaction;
import odgs.listener.FileListener;
import odgs.manager.TransactionManager;

public class ContentsGetTransaction extends Transaction implements FileListener
{
	protected ExplorerNode source;
	protected String path;
	protected long sendedSize;
	protected long totalSize;
	transient private int speed;
	transient private TransactionManager manager;
	transient private List<String> fileNames;
	protected boolean isMarging;
	
	public ContentsGetTransaction(long id)
	{
		super(id);
	}

	@Override
	public String getName()
	{
		return "ContentsGetTransaction";
	}
	
	public void setManager(TransactionManager manager)
	{
		this.manager = manager;
	}

	@Override
	public boolean isFileTrans()
	{
		return true;
	}
	
	public void setSpeed(int speed)
	{
		this.speed = speed;
	}
	
	public int getSpeed()
	{
		return speed;
	}
	
	public void setTotalSize(long totalSize)
	{
		this.totalSize = totalSize;
	}
	
	public long getTotalSize()
	{
		return totalSize;
	}
	
	@Override
	public String toString()
	{
		return "Get Contents : " + source.getName();
	}

	public void setSource(ExplorerNode node)
	{
		this.source = node;
		if(!node.canChildren())
			this.totalSize = ((Contents)node).getSize();
	}

	public void setPath(String path)
	{
		this.path = path;
	}
	
	public String getPath()
	{
		return path;
	}
	
	public ExplorerNode getSource()
	{
		return source;
	}

	public void end(long id, String name)
	{
		if(totalSize <= sendedSize)
		{
			manager.gotContents(this);
		}
	}
	
	public void start(long id, String name){}

	public void sendedSize(long sendedSize, long size)
	{
		this.sendedSize += size;
		this.speed += size;
	}
	
	public void setZero()
	{
		speed = 0;
	}

	public long getSendedSize()
	{
		return sendedSize;
	}
	
	public void addFileName(String name)
	{
		if(fileNames == null)
			fileNames = new ArrayList<String>();
		fileNames.add(name);
	}
	
	public List<String> getFileNames()
	{
		return fileNames;
	}

	public void marge()
	{
		isMarging  = true;
	}
	
	public boolean isMarging()
	{
		return isMarging;
	}
}